function [ww,uu,vv,num_loop] = alg_Closed_Form_FP(N,K,MG,hGG,noise,pow_max)
ww = initialize_w(N,K,MG,hGG,pow_max);

sum_rate_terres_record = [];
fq_record = [];
Lagran_record = [];

f_quadratic_old = 1e-10;
sum_rate_terres_old = 1e-10;
num_loop=0;
while true
    %% update U V W
    [uu,vv] = update_u_v(N,K,ww,hGG,noise);
    [ww,eta] = update_w_CF_FP_bisection(N,K,MG,hGG,uu,vv,pow_max);
    num_loop = num_loop+1;

    %% converge
    sum_rate_terres = cal_sum_rate_terres(N,K,ww,hGG,noise);
    sum_rate_terres_record = [sum_rate_terres_record;sum_rate_terres]; % 用于绘图
%     fprintf('sum_rate_terres = %d\n',sum_rate_terres);
    
    f_quadratic = cal_fq_Closed_Form(N,K,uu,vv,ww,hGG,noise);
    fq_record = [fq_record;f_quadratic]; % 用于绘图
    
    cons_sum_power = cal_sum_power(N,K,ww);
    Lagran_new = f_quadratic + sum(eta.*(pow_max - cons_sum_power));
    Lagran_record = [Lagran_record;Lagran_new]; % 用于绘图
    
%     if abs(f_quadratic-f_quadratic_old) / f_quadratic_old < 0.01
%         fprintf('Closed Form FP terminates: f_quadratic converges\n');
%         break
%     else
%         f_quadratic_old = f_quadratic;
%     end
    
    if abs(sum_rate_terres-sum_rate_terres_old) / sum_rate_terres_old < 0.01
        fprintf('Closed Form FP terminates: sum_rate_terres converges\n');
        break
    else
        sum_rate_terres_old = sum_rate_terres;
    end
end
plot_Closed_Form_FP_conver(sum_rate_terres_record,fq_record,Lagran_record);
end

function plot_Closed_Form_FP_conver(sum_rate_terres_record,fq_record,Lagran_record)
% convergence
% From the convergence of the objective function 
% to see whether the algorithm is effective
ff = figure;
subplot(3,1,1)
plot(1:length(sum_rate_terres_record),sum_rate_terres_record,'x-');hold on
% legend('Original objective','Location','east')
ylabel('Sum rate (bps/Hz)')
% ylabel('Sum rate of the terrestrial network (bps/Hz)')
grid on
set(gca, 'FontName', 'Times New Roman','FontSize',12)
subplot(3,1,2)
plot(1:length(fq_record),fq_record,'o-');hold on
% legend('Transformed objective','Location','east')
ylabel('Transformed objective')
grid on
set(gca, 'FontName', 'Times New Roman','FontSize',12)
subplot(3,1,3)
plot(1:length(Lagran_record),Lagran_record,'>-');hold on
% legend('Lagrangian','Location','east')
ylabel('Lagrangian')
grid on
xlabel('The number of iterations of Closed Form FP')
set(gca, 'FontName', 'Times New Roman','FontSize',12)
set(ff,'Position',[1000,394,640,560]);
end

